package com.hs.shirodemo01.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @ClassName FileDownloadController
 * @Description TODO
 * @Author 郭敖
 * @Date 2023年08月16日 19:29
 * @Version 1.0
 */
@Api(tags = "文件下载接口",value = "文件下载的接口")
@Controller
public class FileDownloadController {

    @ApiOperation(tags = "获取到上传的文件",value = "获取上传的文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "path" ,value = "数据库中保存的图片的路径"),
            @ApiImplicitParam(name = "response", value = "响应体 返回的图片放在响应体中")
    })
    @GetMapping("/image")
    public void downloadImage(@RequestParam("path") String path, HttpServletResponse response) throws IOException {
        File file = new File("d:/"+path);
        FileInputStream fin = new FileInputStream(file);
        // 设置响应头信息
        response.setHeader("Content-Disposition", "filename="+path);//预览
        ServletOutputStream out = response.getOutputStream();
        // 拷贝
        IOUtils.copy(fin,out);
    }
}
